package com.xgkj.diyiketang.activity;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.GridView;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshGridView;
import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.activity.data.VideoAdapter;
import com.xgkj.diyiketang.data.VideoInfo;
import com.xgkj.diyiketang.http.engine.ShowToastTaskEventListener;
import com.xgkj.diyiketang.task.MoreVideoListTask;
import com.xgkj.diyiketang.task.VideoListTask;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by XCKJ_Paladowow on 2016/7/13.
 */
public class AllVideoListActivity extends BaseActivity {
    private Context mContext;
    private PullToRefreshGridView mListView;
    private VideoAdapter mAdapter;
    private int moreVideoTaskId = -1;
    private int size = 10;
    private int currentPage = 1;
    private TextView back;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.allvideolist);
        mContext = this;
        initView();
    }

    private void initView() {
        back = (TextView) findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        mListView = (PullToRefreshGridView) findViewById(R.id.allvideo_list);
        mListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<GridView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<GridView> refreshView) {
                currentPage = 1;
                getMoreVieoList();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<GridView> refreshView) {
                getMoreVieoList();
            }
        });
        mAdapter = new VideoAdapter(mContext);
        mListView.setAdapter(mAdapter);
        getMoreVieoList();
    }

    private void getMoreVieoList() {
        if (currentPage == 0) {
            mListView.onRefreshComplete();
            mListView.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
//            ToastUtil.showMessage(mContext, R.string.no_more);
            return;
        }
        if (moreVideoTaskId != -1) {
            return;
        }
        MoreVideoListTask task = new MoreVideoListTask(size,currentPage,"","","","");
        task.addListener(new MoreListner(mContext));
        moreVideoTaskId = getEatShowApp().getTaskManager().addTask(task);

    }

    class MoreListner extends ShowToastTaskEventListener {

        public MoreListner(Context context) {
            super(context);
        }

        @Override
        public void handleTaskCompleted(int taskId, int result, Object data) {
            super.handleTaskCompleted(taskId, result, data);
            if (taskId == moreVideoTaskId) {
                moreVideoTaskId = -1;
                mListView.onRefreshComplete();
                if (currentPage == 1) {
                    mAdapter.clearData();
                }
                List<VideoInfo> attentionList = new ArrayList<VideoInfo>();
                if (data != null) {
                    attentionList = (List<VideoInfo>) data;
                }

                if (attentionList != null && attentionList.size() > 0) {
                    mAdapter.updateData(attentionList);
                }

                if (currentPage == 1 && mAdapter.getCount() > 0) {
                    mListView.setMode(PullToRefreshBase.Mode.BOTH);
                }

                currentPage = result;
            }

        }

        @Override
        public void handleTaskError(int taskId, int errorCode, String errorMsg) {
            super.handleTaskError(taskId, errorCode, errorMsg);
            if (taskId == moreVideoTaskId) {
                moreVideoTaskId = -1;
                mListView.onRefreshComplete();
            }
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
//        mAdapter.clearData();
        currentPage = 1;
        getMoreVieoList();
    }

}
